/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			flt_regexnc.cc
  \date			Aug 2011
  \author		TNick

  \brief		Contains the implementation for Flt_RegExNC class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include    <QWidget>
#include    <QGroupBox>
#include    <QSettings>
#include    <QVBoxLayout>
#include    <QPlainTextEdit>
#include    <QRegExp>

#include    <data_store/pagedata.h>

#include	"flt_regexnc.h"



/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

class       Flt_RegExNC_Data    {
public:
	QVBoxLayout *verticalLayout;

	QGroupBox * grp_name;
	QGroupBox * grp_cont;

	QPlainTextEdit * tx_name;
	QPlainTextEdit * tx_cont;

};



// the unique instance
Flt_RegExNC *       Flt_RegExNC::uniq_instance = NULL;


/* ------------------------------------------------------------------------- */
Flt_RegExNC::Flt_RegExNC	(void)
{


	QSettings settings;
	settings.beginGroup("Flt_RegExNC");

	flags = (FLG)
			settings.value(QString("flags"),
						   (NAME_ENABLED |
							CONT_ENABLED |
                            FILTER_ENABLED
                            )).toInt();

	s_tx_filt_name =
			settings.value(QString("s_tx_filt_name"),
						   (
							   "XXX"
                               )).toString();
	s_tx_filt_cont =
			settings.value(QString("s_tx_filt_cont"),
						   (
							   "XXX"
                               )).toString();

	settings.endGroup();


    rex__name.setPattern(s_tx_filt_name);
    rex__name.setCaseSensitivity(Qt::CaseInsensitive);
    rex__name.setPatternSyntax(QRegExp::RegExp2);

    rex__cont.setPattern(s_tx_filt_cont);
    rex__cont.setCaseSensitivity(Qt::CaseInsensitive);
    rex__cont.setPatternSyntax(QRegExp::RegExp2);


}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Flt_RegExNC::~Flt_RegExNC	(void)
{

	QSettings settings;
	settings.beginGroup("Flt_RegExNC");

	settings.setValue(QString("flags"), flags);
	settings.setValue(QString("s_tx_filt_name"), s_tx_filt_name);
	settings.setValue(QString("s_tx_filt_cont"), s_tx_filt_cont);

	settings.endGroup();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
Flt_RegExNC* Flt_RegExNC::initFilter                (void)
{
	if (uniq_instance == NULL)
		{
			uniq_instance = new Flt_RegExNC();
			registerFilter(uniq_instance);
		}
	return uniq_instance;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString     Flt_RegExNC::name                       (void)
{
	return QString("RegExp Name & Content");
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool        Flt_RegExNC::active                     (void)
{
	return filtActive();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void *      Flt_RegExNC::createOptions              (QWidget* parent)
{
	Flt_RegExNC_Data * p_data = new Flt_RegExNC_Data();

	p_data->verticalLayout = new QVBoxLayout(parent);

	p_data->grp_name = new QGroupBox(QString("Filter by name"));
    p_data->grp_name->setCheckable(true);
    p_data->grp_name->setChecked(nameEnabled());
	p_data->verticalLayout->addWidget(p_data->grp_name);
	QVBoxLayout * v1 = new QVBoxLayout(p_data->grp_name);
	p_data->grp_name->setLayout(v1);
	p_data->tx_name = new QPlainTextEdit(s_tx_filt_name);
	v1->addWidget(p_data->tx_name);
    p_data->grp_name->show();

	p_data->grp_cont = new QGroupBox(QString("Filter by content"));
	p_data->verticalLayout->addWidget(p_data->grp_cont);
    p_data->grp_cont->setCheckable(true);
    p_data->grp_cont->setChecked(contEnabled());
	QVBoxLayout * v2 = new QVBoxLayout(p_data->grp_cont);
	p_data->grp_cont->setLayout(v2);
	p_data->tx_cont = new QPlainTextEdit(s_tx_filt_cont);
	v2->addWidget(p_data->tx_cont);
    p_data->grp_cont->show();

	parent->setLayout(p_data->verticalLayout);

	return p_data;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        Flt_RegExNC::saveOptions                (
    QWidget* parent, bool active_sts, void* data)
{
	Q_UNUSED(parent);

	Flt_RegExNC_Data * p_data = (Flt_RegExNC_Data *) data;

	setNameEnabled(p_data->grp_name->isChecked());
	setContEnabled(p_data->grp_cont->isChecked());

	s_tx_filt_name = p_data->tx_name->toPlainText();
	s_tx_filt_cont = p_data->tx_cont->toPlainText();

    rex__name.setPattern(s_tx_filt_name);
    rex__cont.setPattern(s_tx_filt_cont);

    setFiltActive(active_sts);

	delete p_data;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool        Flt_RegExNC::passesFilter               (PageData * pg_data)
{

	if (pg_data == NULL) return false;


    /* check the title for a match */
    if (nameEnabled())
        {
            int res = rex__name.indexIn(pg_data->displName());
            if (res != -1)
                {
                    return true;
                }
        }

    /* check the content for a match */
    if (contEnabled())
        {
            int res = rex__cont.indexIn(pg_data->pageContent());
            if (res != -1)
                {
                    return true;
                }
        }

	return false;
}
/* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
